// userinfo.js
import api from "../../../utils/api";
import util from "../../../utils/util";
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isCanSubmit:false,
    birthday:"",
    isMan:false,
    nation: "",
    ageStart:"",//年龄下限
    ageEnd:"",//年龄上限
    statureStart:"",//身高下限
    statureEnd:"",//身高上限
    incomeStart:"",//工资下限
    incomeEnd:"",//收入上限
    tagShow:false,//是否显示标签框
    tags:[{
      tagValue:"气质",
      selected:false
    },{
        tagValue: "高冷",
        selected: false
    },{
        tagValue: "开朗",
        selected: false
    }, {
      tagValue: "知性",
      selected: false
    }, {
      tagValue: "御姐",
      selected: false
    }, {
      tagValue: "安静",
      selected: false
    }, {
      tagValue: "85后",
      selected: false
    }, {
      tagValue: "90后",
      selected: false
    }, {
      tagValue: "乖巧",
      selected: false
    }, {
      tagValue: "慢热型",
      selected: false
    }, {
      tagValue: "主动型",
      selected: false
    }],
    selectedTags:[],
    hobbys: ["唱歌", "跳舞", "吉他", "架子鼓", "钢琴", "古筝", "二胡", "马头琴", "书法", "绘画", "模型", "陶瓷古董", "玉器", "黄金", "白银", "青铜器", "翡翠", "玛瑙", "珍珠", "武术", "搏击", "游泳", "篮球", "台球", "乒乓球", "排球", "足球", "网球", "保龄球", "橄榄球", "逛街", "跑步", "骑车，旅游", "登山", "英雄联盟", "王者荣耀", "斗地主", "打麻将"],
    nations: ["汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族", "朝鲜族", "满族", "侗族", "瑶族", "白族", "土家族",
      "哈尼族", "哈萨克族", "傣族", "黎族", "傈僳族", "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族",
      "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族", "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族", "塔吉克族", "怒族", "乌孜别克族",
      "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族", "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族"],

    religions: ["无","基督教", "伊斯兰教", "佛教", "其他"],
    degrees:["小学","初中","高中","大专","本科","硕士","博士","博士后"],
    religion: "",
    //个人信息
    userInfo:{
      id:app.globalData.openid,
      name:"",
      sex:"",
      age:0,
      phone:"",
      birthplace:"",
      nation:"",
      religion:"",
      stature:0,//身高
      weight:0,//体重
      marital:"",//婚姻状况
      censusregister:"",//户籍
      degree:"",//教育背景
      income:0,//年收入
      site:"",//住址
      compony:"",
      profession:"",//职业
      intro:"",//个人简介
      my_heart:"",//我的心声
      my_hobby:"",//我的爱好
      my_strengths:"",//我的长处
      school:"",//毕业学校
      wxNumber:"",//微信号
      house:"",//房
      car:"",//车
      /**
       * 择偶信息
       */
      zage:"",//择偶的年龄区间
      zincome:"",//择偶的收入
      zeducation:"",//择偶的学历
      zhouse:"",//择偶有无房子
      zcar:"",//择偶有无车辆
      zcensus:"",//择偶户籍地
      // zdegree:"",//择偶学历
    },
   
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    util.isLogin();
    //加载个人信息
    wx.request({
      url: api.getUserById,
      data:{
        id:app.globalData.openid
      },
      type:"POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success:function(response){
        console.log("获取个人信息成功！");
        // console.log();
        var info = response.data.data[0];
        var zstature = info.zstature.split("-");
        var zage = info.zage.split("-");
        var zincome = info.zincome.split("-");
        
        that.setData({
          userInfo: response.data.data[0],
          ageStart:zage[0],
          ageEnd:zage[1],
          incomeStart:zincome[0],
          incomeEnd:zincome[1],
          
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // var nations = ;
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 选择出生日期
   */
  birthdayChange:function(e){
    this.data.userInfo.birthplace = e.detail.value;
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  /**
   * 选择民族
   */
  changeNation:function(e){
    console.log(this.data.nations[e.detail.value]);
    this.data.userInfo.nation = this.data.nations[e.detail.value];
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  /**
   * 选择宗教
   */
  changeReligion: function (e) {
    console.log(this.data.nations[e.detail.value]);
    this.data.userInfo.religion = this.data.religions[e.detail.value];
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  /**
   * 选择性别
   */
  changeSex:function(e){
    console.log("选择性别");
    console.log(e);
    this.data.userInfo.sex = e.detail.value.trim();
  
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  /**
   * 选择婚姻状况
   */
  changeMarital:function(e){
    this.data.userInfo.marital = e.detail.value.trim();
    this.setData({
      userInfo:this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  /**
   * 学历信息
   */
  changeDegree:function(e){
    console.log(this.data.degrees[e.detail.value]);
    this.data.userInfo.degree = this.data.degrees[e.detail.value];
    this.setData({
      userInfo: this.data.userInfo,
      // isCanSubmit: this.canSubmit()
    });
  },
  changezDegree: function (e) {
    console.log(this.data.degrees[e.detail.value]);
    this.data.userInfo.zeducation = this.data.degrees[e.detail.value];
    this.setData({
      userInfo: this.data.userInfo,
      // isCanSubmit: this.canSubmit()
    });
  },
  /**
   * 房车选择
   */
  changeHouse:function(e){
    console.log("房");
    console.log(e.detail.value);
    if (e.detail.value.length == 1){
      this.data.userInfo.house = "房";
    }else{
      this.data.userInfo.house = "";
    }
    this.setData({
      userInfo:this.data.userInfo
    });
  },
 
  /**
 * 房车选择
 */
  changeCar: function (e) {
    console.log("车");
    console.log(e.detail.value);
    if (e.detail.value.length == 1) {
      this.data.userInfo.car = "车";
    } else {
      this.data.userInfo.car = "";
    }
    this.setData({
      userInfo: this.data.userInfo
    });
  },
  /**
 * 房车选择
 */
  changezHouse: function (e) {
    console.log("相亲房");
    console.log(e.detail.value);
    if (e.detail.value.length == 1) {
      this.data.userInfo.zhouse = "房";
    } else {
      this.data.userInfo.zhouse = "";
    }
    this.setData({
      userInfo: this.data.userInfo
    });
  },
  /**
* 房车选择
*/
  changezCar: function (e) {
    console.log("相亲车");
    console.log(e.detail.value);
    if (e.detail.value.length == 1) {
      this.data.userInfo.zcar = "车";
    } else {
      this.data.userInfo.zcar = "";
    }
    this.setData({
      userInfo: this.data.userInfo
    });
  },
  /**
   * 提交信息
   */
  submitUserInfo:function(){

    this.data.userInfo.id = app.globalData.openid;
    this.setData({
      userInfo:this.data.userInfo
    });
    console.log("提交信息");
    console.log(this.data.userInfo);
    wx.request({
      url: api.updateUserById,
      data:this.data.userInfo,
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(response){
        console.log("修改个人信息成功！");
        console.log(response.data);
      }
    })
  },
  /**
   * 文本框输入双向绑定
   */
  nameInput:function(e){
    this.data.userInfo.name = e.detail.value.trim();
    this.setData({
      userInfo:this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  ageInput: function (e) {
    this.data.userInfo.age = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  wxNumberInput: function (e) {
    this.data.userInfo.wxNumber = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit: this.canSubmit()
    });
  },
  birthdayInput: function (e) {
    this.data.userInfo.birthday = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  statureInput: function (e) {
    this.data.userInfo.stature = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  weightInput: function (e) {
    this.data.userInfo.weight = e.detail.value ? e.detail.value : '';
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  censusregisterInput: function (e) {
    this.data.userInfo.censusregister = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },

  degreeInput: function (e) {
    this.data.userInfo.degree = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  incomeInput: function (e) {
    this.data.userInfo.income = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  phonenumberInput: function (e) {
    this.data.userInfo.phone = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit: this.canSubmit()
    });
  },
  siteInput: function (e) {
    this.data.userInfo.site = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  schoolInput: function (e) {
    this.data.userInfo.school = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      // isCanSubmit: this.canSubmit()
    });
  },
  componyInput: function (e) {
    this.data.userInfo.compony = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  professionInput: function (e) {
    this.data.userInfo.profession = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  introInput: function (e) {
    this.data.userInfo.intro = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  my_heartInput: function (e) {
    this.data.userInfo.my_heart = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  my_hobyInput: function (e) {
    this.data.userInfo.my_hobby = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  my_strengthsInput: function (e) {
    this.data.userInfo.my_strengths = e.detail.value.trim();
    this.setData({
      userInfo: this.data.userInfo,
      isCanSubmit:this.canSubmit()
    });
  },
  ageStartInput: function (e) {
    this.data.ageStart = e.detail.value.trim();
    this.data.userInfo.zage = this.data.ageStart + "-" + this.data.ageEnd;
    this.setData({
      userInfo: this.data.userInfo,
      // isCanSubmit: this.canSubmit()
    });
  },
  ageEndInput: function (e) {
    this.data.ageEnd = e.detail.value.trim();
    this.data.userInfo.zage = this.data.ageStart + "-" + this.data.ageEnd;
    this.setData({
      userInfo: this.data.userInfo,
      // isCanSubmit: this.canSubmit()
    });
  },
  statureStartInput: function (e) {
    this.data.statureStart = e.detail.value.trim();
    this.data.userInfo.zstature = this.data.statureStart + "-" + this.data.statureEnd;
    this.setData({
      userInfo: this.data.userInfo,
      // isCanSubmit: this.canSubmit()
    });
  },
 statureEndInput: function (e) {
   this.data.statureEnd = e.detail.value.trim();
   this.data.userInfo.zstature = this.data.statureStart + "-" + this.data.statureEnd;
    this.setData({
      userInfo: this.data.userInfo,
      // isCanSubmit: this.canSubmit()
    });
  },
  incomeStartInput: function (e) {
    this.data.incomeStart = e.detail.value.trim();
    this.data.userInfo.zincome = this.data.incomeStart + "-" + this.data.incomeEnd;
    this.setData({
      userInfo: this.data.userInfo,
      // isCanSubmit: this.canSubmit()
    });
  },
  incomeEndInput: function (e) {
    this.data.incomeEnd = e.detail.value.trim();
    this.data.userInfo.zincome = this.data.incomeStart + "-" + this.data.incomeEnd;
    this.setData({
      userInfo: this.data.userInfo,
      // isCanSubmit: this.canSubmit()
    });
  },
  zcensusInput:function(e){
    this.data.userInfo.zcensus = e.detail.value.trim();
    this.setData({
      userInfo:this.data.userInfo
    });
  },

  /**
   * 判断可不可以提交
   */
  canSubmit:function(){
    if (this.data.userInfo.name == ""){
        return false;
    }
    if (this.data.userInfo.sex == ""){
        return false;
    }
    if (this.data.userInfo.age == ""){
        return false;
    }
    if (this.data.userInfo.birthday == ""){
        return false;
    }
    if (this.data.userInfo.nation == ""){
        return false;
    }
    if (this.data.userInfo.religion == ""){
        return false;
    }
    if (this.data.userInfo.stature == ""){
        return false;
    }
    if (this.data.userInfo.weight == ""){
        return false;
    }
    if (this.data.userInfo.marital == ""){
        return false;
    }
    if (this.data.userInfo.censusregister == ""){
        return false;
    }
    if (this.data.userInfo.degree == ""){
        return false;
    }
    if (this.data.userInfo.site == ""){
        return false;
    }
    if (this.data.userInfo.compony == ""){
        return false;
    }
    if (this.data.userInfo.profession == ""){
        return false;
    }
    if (this.data.userInfo.intro == ""){
        return false;
    }
    if (this.data.userInfo.my_heart == ""){
        return false;
    }
    if (this.data.userInfo.my_hoby == ""){
        return false;
    }
    if (this.data.userInfo.my_heart == ""){
        return false;
    }
    if (this.data.userInfo.my_strengths == ""){
        return false;
    }
    return true;
  },
  /**
   * 标签
   */
  tapTag:function(e){
    
    var index = e.currentTarget.dataset.index;
    if (this.data.selectedTags.length >= 3 && !this.data.tags[index].selected) {
      return;
    }
    this.data.tags[index].selected = !this.data.tags[index].selected;
    this.setData({
      tags:this.data.tags,
      selectedTags: this.calcSelectedTag()
    });
  },
  calcSelectedTag:function(){
    var arr = [];
    this.data.tags.forEach(function(item){
      if(item.selected){
        arr.push(item);
      }
    });
    return arr;
  },
  /**
   * 确定
   */
  addTag:function(){
    this.setData({
      tagShow:false
    });
  },
  showTags:function(){
    this.setData({
      tagShow:true
    });
  }
})